package com.snifferx.util.Converter;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import org.jboss.logging.Logger;

import com.snifferx.domain.AuthorityEntity;
import com.snifferx.managedbeans.RoleAccessBean;
import com.snifferx.services.RolePermissionService;

@ManagedBean(name = "RoleConverter")
@SessionScoped
@FacesConverter("RoleConverter")
public class RoleConverter implements Converter , Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2719746113390811043L;

	private static Logger log = Logger.getLogger(RoleConverter.class);

	@ManagedProperty(value = "#{rolePermissionService}")
	private RolePermissionService rolePermissionService;
	// 应该将TempRoleView也放进来.
	@ManagedProperty(value = "#{roleAccessBean}")
	private RoleAccessBean roleAccessBean;

	@Override
	// 根据string获得当前的role对象
	public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) throws ConverterException {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
                String  roleIno = submittedValue.toString();

                for (AuthorityEntity p : roleAccessBean.getSelectedTempRoleView().getOptionalRoles()) {
                	log.debug("getAsObject:" + p.getUser().getUsername());
                	//get the first split :
                	log.debug("first split:" + roleIno.substring(0, roleIno.indexOf('-')));
                	//get the second split:
                	log.debug("second split:" + roleIno.substring(roleIno.indexOf('-') + 1));
                    if (p.getAuthority().equals(roleIno.substring(0, roleIno.indexOf('-'))) && p.getUser().getUsername().equals(roleIno.substring(roleIno.indexOf('-') + 1))) {
                        return p;
                    }
                }
                
            } catch(Exception e) {
            	e.printStackTrace();
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid role"));
            }
        }

        return null;
	}

	@Override
	public String getAsString(FacesContext context, final UIComponent component, Object value) throws ConverterException {
        if (value == null || value.equals("")) {
            return "";
        } else {
        	log.debug("getAsString :AuthorityEntity = " + ((AuthorityEntity)value).getAuthority() + ", useName = " + ((AuthorityEntity)value).getUser().getUsername());
        	
        	log.debug("return string :" + ((AuthorityEntity) value).getAuthority() +"-" +  ((AuthorityEntity)value).getUser().getUsername());
        	
        	//return role "string-userName"
            return String.valueOf(((AuthorityEntity) value).getAuthority() +"-" +  ((AuthorityEntity)value).getUser().getUsername());
        }
	}

	public RolePermissionService getRolePermissionService() {
		return rolePermissionService;
	}

	public void setRolePermissionService(RolePermissionService rolePermissionService) {
		this.rolePermissionService = rolePermissionService;
	}

	public RoleAccessBean getRoleAccessBean() {
		return roleAccessBean;
	}

	public void setRoleAccessBean(RoleAccessBean roleAccessBean) {
		this.roleAccessBean = roleAccessBean;
	}

}